package com.platform.service.impl;

import com.platform.dao.OrderDao;
import com.platform.entity.LogisticsEntity;
import com.platform.entity.OrderDetailEntity;
import com.platform.entity.OrderEntity;
import com.platform.entity.UserAssetEntity;
import com.platform.entity.excel.OrderExcel;
import com.platform.service.LogisticsService;
import com.platform.service.OrderDetailService;
import com.platform.service.OrderService;
import com.platform.service.UserAssetService;
import com.platform.util.OrderStatusEnum;
import com.platform.utils.DateUtils;
import com.platform.utils.Query;
import com.platform.utils.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 订单管理:商城购买订单
 */
@Slf4j
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private UserAssetService userAssetService;


    @Override
    public OrderEntity queryObject(Integer id) {
        OrderEntity order = orderDao.queryObject(id);
        return order;
    }

    @Override
    public List<OrderEntity> queryList(Map<String, Object> map) {

        //map.put("merchantId",1);
        Object date = map.get("date");
        if(date!=null && date != ""){
            Date tomorrow = DateUtils.minusDateForDay(date.toString(),1);
            map.put("tomorrow",tomorrow);
        }
        return orderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        //map.put("merchantId",1);
        return orderDao.queryTotal(map);
    }

    @Override
    public int save(OrderEntity order) {
//        order.setMerchantId(1);
//        order.setMerchantName("动动体育");
        return orderDao.save(order);
    }

    @Override
    public int update(OrderEntity order) {
//        order.setMerchantId(1);
//        order.setMerchantName("动动体育");
        return orderDao.update(order);
    }

    @Override
    public int delete(Integer id) {
        return orderDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return orderDao.deleteBatch(ids);
    }

    /**
     * @Description: 发货调用
     * @Date: 2019/3/22 14:43
     * @Param: [orderNo]
     * @Return: int
     * @Exception:
     */
    @Override
    public int confirm(String orderNo,String logisticsno) {
        return orderDao.updateStatus(orderNo, OrderStatusEnum.ORDER_STATUS_CONFIRM.getKey(),logisticsno);
    }


    @Transactional
    @Override
    public int sendGoods(OrderEntity order) {
        Integer payStatus = order.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款！");
        }
        order.setStatus(5);// 发货
        LogisticsEntity entity = new LogisticsEntity();
        entity.setName("韵达");
        entity.setNum(order.getNumber());
        entity.setOrderno(order.getOrderNo());
        entity.setStatu(1);
        entity.setPid(order.getProductId());
        entity.setUserId(order.getUserId());
        entity.setPhone(order.getUserPhone());
        entity.setUserName(order.getUserName());
        entity.setPname(order.getProductName());
        entity.setLogisticsNo(order.getLogisticsno());
        orderDao.update(order);

        return logisticsService.save(entity);
    }

    @Override
    public List<OrderEntity> querySpcList(Query query) {
        Object date = query.get("date");
        if(date!=null && date != ""){
            Date tomorrow = DateUtils.minusDateForDay(date.toString(),1);
            query.put("tomorrow",tomorrow);
        }
        //query.put("merchantId",1);
        return orderDao.querySpcList(query);
    }

    @Override
    public int querySpcTotal(Query query) {
        //query.put("merchantId",1);
        return orderDao.querySpcTotal(query);
    }

    @Override
    public OrderEntity querySpcObject(Integer id) {

        return orderDao.queryObject(id);
    }

    @Transactional
    @Override
    public boolean confirmSpc(Integer id) {
        log.info("确认发货begin");
        OrderEntity entity = orderDao.queryObject(id);
        entity.setStatus(4);
        entity.setPayStatus(2);
        int i =orderDao.confirmSpc(entity);
        return i> 0;
    }

    @Override
    public boolean rejectSpc(Integer rowId) {
        OrderEntity entity = orderDao.queryObject(rowId);
        // 添加商户资产
        UserAssetEntity asset = userAssetService.queryObjectForUid(entity.getUserId());
        asset.setCoin(asset.getCoinFrozen().subtract(entity.getAmount()));
        boolean result = userAssetService.updateAsset(asset);
        if(!result){
            return false;
        }
        entity.setStatus(3);//取消
        return orderDao.update(entity)>0;
    }

    @Override
    public List<OrderExcel> queryListExcel(String orderNo, String userPhone, Integer payStatus, Integer status, Integer index, Integer size,Integer type,Integer payWay,Integer merchantId) {
        Date tomorrow = new Date();
        int start =(index-1)*size;
        return orderDao.queryListExcel( orderNo,  userPhone,  payStatus,  status,  start,  size,type,payWay,merchantId);
    }

    @Override
    public Map<String, Object> queryOrderMap(Integer merchantId) {
        return orderDao.queryOrderMap(merchantId);
    }


}
